﻿using System;
using System.Xml;
using System.Xml.Serialization;

public static class XmlTools {
	public static void Serialize(Object objectToSerialize, string xmlDocFullName) {
		var xmlDoc = new XmlDocument();
		using(var xmlWriter = xmlDoc.CreateNavigator().AppendChild()) {
			var xmlSerializer = new XmlSerializer(objectToSerialize.GetType());
			xmlSerializer.Serialize(xmlWriter, objectToSerialize);
		}
		xmlDoc.Save(xmlDocFullName);
	}

	public static T Deserialize<T>(XmlNode xmlNode) {
		if(xmlNode == null) {
			throw new Exception("XmlNode is null");
		}
		var serializer = new XmlSerializer(typeof(T));
		var reader = new XmlNodeReader(xmlNode);
		return (T)serializer.Deserialize(reader);
	}

	public static LibraryModel GetLibModel(string xmlDocPath) {
		var xmlDoc = new XmlDocument();
		xmlDoc.Load(xmlDocPath);
		return Deserialize<LibraryModel>(xmlDoc.DocumentElement);
	}
}